package SparqlClient;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
    	Test sp = new Test();
    	
        //SparqlClient sparqlClient = new SparqlClient("192.168.250.91:8080/sparqluedo");
    	SparqlClient sparqlClient = new SparqlClient("127.0.0.1:3030/space");

        String query = "ASK WHERE { ?s ?p ?o }";
        boolean serverIsUp = sparqlClient.ask(query);
        if (serverIsUp) {
            System.out.println("server is UP");
            
            // Requête 9
            String req = "acteurs, joué avec, Omar Sy";
        	String[] motReq = MotsCles(req);
            /*query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
		    		"PREFIX owl: <http://www.w3.org/2002/07/owl#>" +
		    		"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
		    		"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>" +
		    		"PREFIX onto: <http://www.irit.fr/recherches/MELODI/ontologies/FilmographieV1.owl#>"
		    		+"SELECT distinct ?p WHERE{"
		    		+"{onto:"+motReq[0]+" onto:aJoueAvec ?o."
		    		+"?o rdfs:label ?p}"
		    		+ "UNION"
		    		+"{onto:"+motReq[2]+" onto:aJoueAvec ?o."
		    		+"?o rdfs:label ?p.}"
		    		+"}";*/
        	// On recherche le type de chaque mot clé
        	query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
		    		"PREFIX owl: <http://www.w3.org/2002/07/owl#>" +
		    		"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
		    		"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>" +
		    		"PREFIX onto: <http://www.irit.fr/recherches/MELODI/ontologies/FilmographieV1.owl#>"
		    		+"SELECT distinct ?type WHERE{"
		    		+"?value rdfs:label '"+motReq[2]+"'."
		    		+"?value rdf:type ?type."
		    		+"}";
            
            		
            
            System.out.println(query);




            sp.results(sparqlClient,query);

        } else {
            System.out.println("service is DOWN");
        }
    }
    
	private static String[] MotsCles(String str){
		//String sansEspace = str.replaceAll("\\s", "");
    	String[] mot = str.split(", ");
    	for (int i=0; i<mot.length; i++){
    		String rempl = mot[i];
    		System.out.println(rempl);
    	}
		return mot;  	
    }
    
	private void typeDuMot (String[] str){
		String req = "acteurs, joué avec, Omar Sy";
		str=MotsCles(req);
	}
    private  void results(SparqlClient sparqlClient, String query) {
    		//int cpt=0;
    		//List<String> resultList = new ArrayList<String>();
            Iterable<Map<String, String>> results = sparqlClient.select(query);
            //System.out.println(sparqlClient.ask(query));
            System.out.println("Evaluation de la requête");
            for (Map<String, String> result : results) {
            	//System.out.println(result);
            	//cpt++;
                //System.out.println(""+cpt+": "+result.get("x")); // Affichage résultat
            	System.out.println(result.get("type")); // Affichage résultat
               //resultList.add(result.get("p"));
            }
            
            //System.out.println(resultList);
			//return resultList;
    } 
    

}
